1) パッケージの導入と準備

パッケージのインストール

install.packages("readxl", repos = "https://cloud.r-project.org")
install.packages("dplyr", repos = "https://cloud.r-project.org")

利用準備

library(readxl)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union

Excelファイルの読み込み

df <- read_excel("X_ray_4040.xlsx")

データの前処理(日本語化,順序化)

df_jp <- df %>%
  mutate(
    `介入の段階` = factor(
      recode(phase, "before" = "スケッチ前", "after" = "スケッチ後"),
      levels = c("スケッチ後", "スケッチ前")
    ),
    `造影効果` = factor(
      recode(as.character(contrast), "1" = "良", "0" = "不良"),
      levels = c("良", "不良")
    ),
    `描出範囲` = factor(
      recode(as.character(depiction), "1" = "良", "0" = "不良"),
      levels = c("良", "不良")
    )
  )

2) 分割表の作成

スケッチ前後の造影効果の変化

cross1 <- addmargins(table(df_jp$介入の段階, df_jp$造影効果))
rownames(cross1)[rownames(cross1) == "Sum"] <- "合計"
colnames(cross1)[colnames(cross1) == "Sum"] <- "合計"
cross1
##             
##              良 不良 合計
##   スケッチ後 36    4   40
##   スケッチ前 19   21   40
##   合計       55   25   80

スケッチ前後の描出範囲の変化

cross2 <- addmargins(table(df_jp$介入の段階, df_jp$描出範囲))
rownames(cross2)[rownames(cross2) == "Sum"] <- "合計"
colnames(cross2)[colnames(cross2) == "Sum"] <- "合計"
cross2
##             
##              良 不良 合計
##   スケッチ後 35    5   40
##   スケッチ前 22   18   40
##   合計       57   23   80

3) Fisherの正確確率検定

スケッチ前後の造影効果の変化

fisher.test(cross1)
## 
##  Fisher's Exact Test for Count Data
## 
## data:  cross1
## p-value = 0.001542
## alternative hypothesis: two.sided

スケッチ前後の描出範囲の変化

fisher.test(cross2)
## 
##  Fisher's Exact Test for Count Data
## 
## data:  cross2
## p-value = 0.03286
## alternative hypothesis: two.sided